home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / demo / wemdemo4.zip / INFO / ELISP.7 < prev    next >
Text File  |  1994-09-21  |  52KB  |  1,259 lines

  1. Info file elisp, produced by Makeinfo, -*- Text -*- from input file
  2. elisp.texi.
  3.  
  4.    This file documents GNU Emacs Lisp.
  5.  
  6.    This is edition 1.03 of the GNU Emacs Lisp Reference Manual,   for
  7. Emacs Version 18.
  8.  
  9.    Published by the Free Software Foundation, 675 Massachusetts
  10. Avenue,  Cambridge, MA 02139 USA
  11.  
  12.    Copyright (C) 1990 Free Software Foundation, Inc.
  13.  
  14.    Permission is granted to make and distribute verbatim copies of
  15. this manual provided the copyright notice and this permission notice
  16. are preserved on all copies.
  17.  
  18.    Permission is granted to copy and distribute modified versions of
  19. this manual under the conditions for verbatim copying, provided that
  20. the entire resulting derived work is distributed under the terms of a
  21. permission notice identical to this one.
  22.  
  23.    Permission is granted to copy and distribute translations of this
  24. manual into another language, under the above conditions for modified
  25. versions, except that this permission notice may be stated in a
  26. translation approved by the Foundation.
  27.  
  28.  
  29. 
  30. File: elisp,  Node: Setting Variables,  Next: Variable Scoping,  Prev: Accessing Variables,  Up: Variables
  31.  
  32. How to Alter a Variable Value
  33. =============================
  34.  
  35.    The usual way to change the value of a variable is with the
  36. special form `setq'.  When you need to compute the choice of variable
  37. at run time, use the function `set'.
  38.  
  39.  * Special Form: setq [SYMBOL FORM]...
  40.      This special form is the most common method of changing a
  41.      variable's value.  Each SYMBOL is given a new value, which is
  42.      the result of evaluating the corresponding FORM.  The most-local
  43.      existing binding of the symbol is changed.
  44.  
  45.      The value of the `setq' form is the value of the last FORM.
  46.  
  47.           (setq x (1+ 2))
  48.                => 3
  49.           x                     ; `x' now has a global value.
  50.                => 3
  51.           (let ((x 5)) 
  52.             (setq x 6)          ; The local binding of `x' is set.
  53.             x)
  54.                => 6
  55.           x                     ; The global value is unchanged.
  56.                => 3
  57.  
  58.      Note that the first FORM is evaluated, then the first SYMBOL is
  59.      set, then the second FORM is evaluated, then the second SYMBOL
  60.      is set, and so on:
  61.  
  62.           (setq x 10            ; Notice that `x' is set
  63.                 y (1+ x))       ; before the value of `y' is computed.
  64.                => 11
  65.  
  66.  * Function: set SYMBOL VALUE
  67.      This function sets SYMBOL's value to VALUE, then returns VALUE. 
  68.      Since `set' is a function, the expression written for SYMBOL is
  69.      evaluated to obtain the symbol to be set.
  70.  
  71.      The most-local existing binding of the variable is the binding
  72.      that is set; shadowed bindings are not affected.  If SYMBOL is
  73.      not actually a symbol, a `wrong-type-argument' error is signaled.
  74.  
  75.           (set one 1)
  76.           error--> Symbol's value as variable is void: one
  77.           (set 'one 1)
  78.                => 1
  79.           (set 'two 'one)
  80.                => one
  81.           (set two 2)            ; `two' evaluates to symbol `one'.
  82.                => 2
  83.           one                    ; So it is `one' that was set.
  84.                => 2
  85.           (let ((one 1))         ; This binding of `one' is set,
  86.             (set 'one 3)         ; not the global value.
  87.             one)
  88.                => 3
  89.           one
  90.                => 2
  91.  
  92.      Logically speaking, `set' is a more fundamental primitive that
  93.      `setq'.  Any use of `setq' can be trivially rewritten to use
  94.      `set'; `setq' could even be defined as a macro, given the
  95.      availability of `set'.  However, `set' itself is rarely used;
  96.      beginners hardly need to know about it.  It is needed only when
  97.      the choice of variable to be set is made at run time.  For
  98.      example, the command `set-variable', which reads a variable name
  99.      from the user and then sets the variable, needs to use `set'.
  100.  
  101.           Common Lisp note: in Common Lisp, `set' always changes the
  102.           symbol's special value, ignoring any lexical bindings.  In
  103.           Emacs Lisp, all variables and all bindings are special, so
  104.           `set' always affects the most local existing binding.
  105.  
  106.  
  107. 
  108. File: elisp,  Node: Variable Scoping,  Next: Buffer-Local Variables,  Prev: Setting Variables,  Up: Variables
  109.  
  110. Scoping Rules for Variable Bindings
  111. ===================================
  112.  
  113.    A given symbol `foo' may have several local variable bindings,
  114. established at different places in the Lisp program, as well as a
  115. global binding.  The most recently established binding takes
  116. precedence over the others.
  117.  
  118.    Local bindings in Emacs Lisp have "indefinite scope" and "dynamic
  119. extent".  "Scope" refers to *where* textually in the source code the
  120. binding can be accessed.  Indefinite scope means that any part of the
  121. program can potentially access the variable binding.  "Extent" refers
  122. to *when*, as the program is executing, the binding exists.  Dynamic
  123. extent means that the binding lasts as long as the activation of the
  124. construct that established it.
  125.  
  126.    The combination of dynamic extent and indefinite scope is called
  127. "dynamic scoping".  By contrast, most programming languages use
  128. "lexical scoping", in which references to a local variable must be
  129. textually within the function or block that binds the variable.
  130.  
  131.      Common Lisp note: variables declared "special" in Common Lisp
  132.      are dynamically scoped like variables in Emacs Lisp.
  133.  
  134. * Menu:
  135.  
  136. * Scope::          Scope means where in the program a value is visible.
  137.                      Comparison with other languages.
  138. * Extent::         Extent means how long in time a value exists.
  139. * Impl of Scope::  Two ways to implement dynamic scoping.
  140. * Using Scoping::  How to use dynamic scoping carefully and avoid problems.
  141.  
  142.  
  143. 
  144. File: elisp,  Node: Scope,  Next: Extent,  Prev: Variable Scoping,  Up: Variable Scoping
  145.  
  146. Scope
  147. -----
  148.  
  149.    Emacs Lisp uses "indefinite scope" for local variable bindings. 
  150. This means that any function anywhere in the program text might
  151. access a given binding of a variable.  Consider the following
  152. function definitions:
  153.  
  154.      (defun binder (x)  ; `x' is bound in `binder'.
  155.         (foo 5))        ; `foo' is some other function.
  156.      
  157.      (defun user ()     ; `x' is used in `user'.
  158.        (list x))
  159.  
  160.    In a lexically scoped language, the binding of `x' from `binder'
  161. would never be accessible in `user', because `user' is not textually
  162. contained within the function `binder'.  However, in dynamically
  163. scoped Emacs Lisp, `user' may or may not refer to the binding of `x'
  164. established in `binder', depending on circumstances:
  165.  
  166.    * If we call `user' directly without calling `binder' at all, then
  167.      whatever binding of `x' is found, it cannot come from `binder'.
  168.  
  169.    * If we define `foo' as follows and call `binder', then the
  170.      binding made in `binder' will be seen in `user':
  171.  
  172.           (defun foo (lose)
  173.             (user))
  174.  
  175.    * If we define `foo' as follows and call `binder', then the
  176.      binding made in `binder' *will not* be seen in `user':
  177.  
  178.           (defun foo (x)
  179.             (user))
  180.  
  181.      Here, when `foo' is called by `binder', it binds `x'.  (The
  182.      binding in `foo' is said to "shadow" the one made in `binder'.) 
  183.      Therefore, `user' will access the `x' bound by `foo' instead of
  184.      the one bound by `binder'.
  185.  
  186.  
  187. 
  188. File: elisp,  Node: Extent,  Next: Impl of Scope,  Prev: Scope,  Up: Variable Scoping
  189.  
  190. Extent
  191. ------
  192.  
  193.    "Extent" refers to the time during program execution that a
  194. variable name is valid.  In Emacs Lisp, a variable is valid only
  195. while the form that bound it is executing.  This is called "dynamic
  196. extent".  "Local" or "automatic" variables in most languages,
  197. including C and Pascal, have dynamic extent.
  198.  
  199.    One alternative to dynamic extent is "indefinite extent".  This
  200. means that a variable binding can live on past the exit from the form
  201. that made the binding.  Common Lisp and Scheme, for example, support
  202. this, but Emacs Lisp does not.
  203.  
  204.    To illustrate this, the function below, `make-add', returns a
  205. function that purports to add N to its own argument M.  This would
  206. work in Common Lisp, but it does not work as intended in Emacs Lisp,
  207. because after the call to `make-add' exits, the variable `n' is no
  208. longer bound to the actual argument 2.
  209.  
  210.      (defun make-add (n)
  211.          (function (lambda (m) (+ n m))))  ; Return a function.
  212.           => make-add
  213.      (fset 'add2 (make-add 2))  ; Define function `add2' with `(make-add 2)'.
  214.           => (lambda (m) (+ n m))
  215.      (add2 4)                   ; Try to add 2 to 4.
  216.      error--> Symbol's value as variable is void: n
  217.  
  218.  
  219. 
  220. File: elisp,  Node: Impl of Scope,  Next: Using Scoping,  Prev: Extent,  Up: Variable Scoping
  221.  
  222. Implementation of Dynamic Scoping
  223. ---------------------------------
  224.  
  225.    A simple sample implementation (which is not how Emacs Lisp
  226. actually works) may help you understand dynamic binding.  This
  227. technique is called "deep binding" and was used in early Lisp systems.
  228.  
  229.    Suppose there is a stack of bindings: variable-value pairs.  At
  230. entry to a function or to a `let' form, we can push bindings on the
  231. stack for the arguments or local variables created there.  We can pop
  232. those bindings from the stack at exit from the binding construct.
  233.  
  234.    We can find the value of a variable by searching the stack from
  235. top to bottom for a binding for that variable; the value from that
  236. binding is the value of the variable.  To set the variable, we search
  237. for the current binding, then store the new value into that binding.
  238.  
  239.    As you can see, a function's bindings remain in effect as long as
  240. it continues execution, even during its calls to other functions. 
  241. That is why we say the extent of the binding is dynamic.  And any
  242. other function can refer to the bindings, if it uses the same
  243. variables while the bindings are in effect.  That is why we say the
  244. scope is indefinite.
  245.  
  246.    The actual implementation of variable scoping in GNU Emacs Lisp
  247. uses a technique called "shallow binding".  Each variable has a
  248. standard place in which its current value is always found--the value
  249. cell of the symbol.
  250.  
  251.    In shallow binding, setting the variable works by storing a value
  252. in the value cell.  When a new local binding is created, the local
  253. value is stored in the value cell, and the old value (belonging to a
  254. previous binding) is pushed on a stack.  When a binding is
  255. eliminated, the old value is popped off the stack and stored in the
  256. value cell.
  257.  
  258.    We use shallow binding because it has the same results as deep
  259. binding, but runs faster, since there is never a need to search for a
  260. binding.
  261.  
  262.  
  263. 
  264. File: elisp,  Node: Using Scoping,  Prev: Impl of Scope,  Up: Variable Scoping
  265.  
  266. Proper Use of Dynamic Scoping
  267. -----------------------------
  268.  
  269.    Binding a variable in one function and using it in another is a
  270. powerful technique, but if used without restraint, it can make
  271. programs hard to understand.  There are two clean ways to use this
  272. technique:
  273.  
  274.    * Use or bind the variable only in a few related functions,
  275.      written close together in one file.  Such a variable is used for
  276.      communication within one program.
  277.  
  278.      You should write comments to inform other programmers that they
  279.      can see all uses of the variable before them, and to advise them
  280.      not to add uses elsewhere.
  281.  
  282.    * Give the variable a well-defined, documented meaning, and make
  283.      all appropriate functions refer to it (but not bind it or set
  284.      it) wherever that meaning is relevant.  For example, the
  285.      variable `case-fold-search' is defined as "non-`nil' means
  286.      ignore case when searching"; various search and replace
  287.      functions refer to it directly or through their subroutines, but
  288.      do not bind or set it.
  289.  
  290.      Then you can bind the variable in other programs, knowing
  291.      reliably what the effect will be.
  292.  
  293.  
  294. 
  295. File: elisp,  Node: Buffer-Local Variables,  Prev: Variable Scoping,  Up: Variables
  296.  
  297. Buffer-Local Variables
  298. ======================
  299.  
  300.    Global and local variable bindings are found in most programming
  301. languages in one form or another.  Emacs also supports another,
  302. unusual kind of variable binding: "buffer-local" bindings, which
  303. apply only to one buffer.  Emacs Lisp is meant for programming
  304. editing commands, and having different values for a variable in
  305. different buffers is an important customization method.
  306.  
  307. * Menu:
  308.  
  309. * Intro to Buffer-Local::      Introduction and concepts.
  310. * Creating Buffer-Local::      Creating and destroying buffer-local bindings.
  311. * Default Value::              The default value is seen in buffers
  312.                                  that don't have their own local values.
  313.  
  314.  
  315. 
  316. File: elisp,  Node: Intro to Buffer-Local,  Next: Creating Buffer-Local,  Prev: Buffer-Local Variables,  Up: Buffer-Local Variables
  317.  
  318. Introduction to Buffer-Local Variables
  319. --------------------------------------
  320.  
  321.    A buffer-local variable has a buffer-local binding associated with
  322. a particular buffer.  The binding is in effect when that buffer is
  323. current; otherwise, it is not in effect.  If you set the variable
  324. while a buffer-local binding is in effect, the new value goes in that
  325. binding, so the global binding is unchanged; this means that the
  326. change is visible in that buffer alone.
  327.  
  328.    A variable may have buffer-local bindings in some buffers but not
  329. in others.  The global binding is shared by all the buffers that
  330. don't have their own bindings.  Thus, if you set the variable in a
  331. buffer that does not have a buffer-local binding for it, the new
  332. value is visible in all buffers except those with buffer-local
  333. bindings.  (Here we are assuming that there are no `let'-style local
  334. bindings to complicate the issue.)
  335.  
  336.    The most common use of buffer-local bindings is for major modes to
  337. change variables that control the behavior of commands.  For example,
  338. C mode and Lisp mode both set the variable `paragraph-start' to
  339. specify that only blank lines separate paragraphs.  They do this by
  340. making the variable buffer-local in the buffer that is being put into
  341. C mode or Lisp mode, and then setting it to the new value for that
  342. mode.
  343.  
  344.    The usual way to make a buffer-local binding is with
  345. `make-local-variable', which is what major mode commands use.  This
  346. affects just the current buffer; all other buffers (including those
  347. yet to be created) continue to share the global value.
  348.  
  349.    A more powerful operation is to mark the variable as
  350. "automatically buffer-local" by calling `make-variable-buffer-local'.
  351. You can think of this as making the variable local in all buffers,
  352. even those yet to be created.  More precisely, the effect is that
  353. setting the variable automatically makes the variable local to the
  354. current buffer if it is not already so.  All buffers start out by
  355. sharing the global value of the variable as usual, but any `setq'
  356. creates a buffer-local binding for the current buffer.  The new value
  357. is stored in the buffer-local binding, leaving the (default) global
  358. binding untouched.  The global value can no longer be changed with
  359. `setq'; you need to use `setq-default' to do that.
  360.  
  361.    When a variable has local values in one or more buffers, you can
  362. get Emacs very confused by binding the variable with `let', changing
  363. to a different current buffer in which a different binding is in
  364. effect, and then exiting the `let'.  The best way to preserve your
  365. sanity is to avoid such situations.  If you use `save-excursion'
  366. around each piece of code that changes to a different current buffer,
  367. you will not have this problem.  Here is an example of incorrect code:
  368.  
  369.      (setq foo 'b)
  370.      (set-buffer "a")
  371.      (make-local-variable 'foo)
  372.      (setq foo 'a)
  373.      (let ((foo 'temp))
  374.        (set-buffer "b")
  375.        ...)
  376.      foo => 'a      ; The old buffer-local value from buffer `a'
  377.                            ; is now the default value.
  378.      (set-buffer "a")
  379.      foo => 'temp   ; The local value that should be gone
  380.                            ; is now the buffer-local value in buffer `a'.
  381.  
  382.  But `save-excursion' as shown here avoids the problem:
  383.  
  384.      (let ((foo 'temp))
  385.        (save-excursion
  386.          (set-buffer "b")
  387.          ...))
  388.  
  389.    Local variables in a file you edit are also represented by
  390. buffer-local bindings for the buffer that holds the file within Emacs.
  391. *Note Auto Major Mode::.
  392.  
  393.  
  394. 
  395. File: elisp,  Node: Creating Buffer-Local,  Next: Default Value,  Prev: Intro to Buffer-Local,  Up: Buffer-Local Variables
  396.  
  397. Creating and Destroying Buffer-local Bindings
  398. ---------------------------------------------
  399.  
  400.  * Command: make-local-variable SYMBOL
  401.      This function creates a buffer-local binding for SYMBOL in the
  402.      current buffer.  Other buffers are not affected.  The value
  403.      returned is SYMBOL.
  404.  
  405.      The buffer-local value of SYMBOL starts out as the same value
  406.      SYMBOL previously had.
  407.  
  408.           ;; In buffer `b1':
  409.           (setq foo 5)                ; Affects all buffers.
  410.                => 5
  411.           (make-local-variable 'foo)  ; Now it is local in `b1'.
  412.                => foo
  413.           foo                         ; That did not change the value.
  414.                => 5
  415.           (setq foo 6)                ; Change the value in `b1'.
  416.                => 6
  417.           foo
  418.                => 6
  419.           
  420.           ;; In buffer `b2', the value hasn't changed.
  421.           (save-excursion
  422.             (set-buffer "b2")
  423.             foo)
  424.                => 5
  425.  
  426.  * Command: make-variable-buffer-local SYMBOL
  427.      This function marks SYMBOL automatically buffer-local, so that
  428.      any attempt to set it will make it local to the current buffer
  429.      at the time.
  430.  
  431.      The value returned is SYMBOL.
  432.  
  433.  * Function: buffer-local-variables &optional BUFFER
  434.      This function tells you what the buffer-local variables are in
  435.      buffer BUFFER.  It returns an association list (*note
  436.      Association Lists::.) in which each association contains one
  437.      buffer-local variable and its value.  If BUFFER is omitted, the
  438.      current buffer is used.
  439.  
  440.           (setq lcl (buffer-local-variables))
  441.           => ((fill-column . 75)
  442.               (case-fold-search . t)
  443.               ...
  444.               (mark-ring #<marker at 5454 in buffers.texi>)
  445.               (require-final-newline . t))
  446.  
  447.      Note that storing new values into the CDRs of the elements in
  448.      this list will *not* change the local values of the variables.
  449.  
  450.  * Command: kill-local-variable SYMBOL
  451.      This function deletes the buffer-local binding (if any) for
  452.      SYMBOL in the current buffer.  As a result, the global (default)
  453.      binding of SYMBOL becomes visible in this buffer.  Usually this
  454.      results in a change in the value of SYMBOL, since the global
  455.      value is usually different from the buffer-local value just
  456.      eliminated.
  457.  
  458.      It is possible to kill the local binding of a variable that
  459.      automatically becomes local when set.  This causes the variable
  460.      to show its global value in the current buffer.  However, if you
  461.      set the variable again, this will once again create a local value.
  462.  
  463.      `kill-local-variable' returns SYMBOL.
  464.  
  465.  * Function: kill-all-local-variables
  466.      This function eliminates all the buffer-local variable bindings
  467.      of the current buffer.  As a result, the buffer will see the
  468.      default values of all variables.  This function also resets
  469.      certain other information pertaining to the buffer: its local
  470.      keymap is set to `nil', its syntax table is set to the value of
  471.      `standard-syntax-table', and its abbrev table is set to the
  472.      value of `fundamental-mode-abbrev-table'.
  473.  
  474.      Every major mode command begins by calling this function, which
  475.      has the effect of switching to Fundamental mode and erasing most
  476.      of the effects of the previous major mode.
  477.  
  478.      `kill-all-local-variables' returns `nil'.
  479.  
  480.  
  481. 
  482. File: elisp,  Node: Default Value,  Prev: Creating Buffer-Local,  Up: Buffer-Local Variables
  483.  
  484. The Default Value of a Buffer-Local Variable
  485. --------------------------------------------
  486.  
  487.    The global value of a variable with buffer-local bindings is also
  488. called the "default" value, because it is the value that is in effect
  489. except when specifically overridden.
  490.  
  491.    The functions `default-value' and `setq-default' allow you to
  492. access and change the default value regardless of whether the current
  493. buffer has a buffer-local binding.  For example, you could use
  494. `setq-default' to change the default setting of `paragraph-start' for
  495. most buffers; and this would work even when you are in a C or Lisp
  496. mode buffer which has a buffer-local value for this variable.
  497.  
  498.  * Function: default-value SYMBOL
  499.      This function returns SYMBOL's default value.  This is the value
  500.      that is seen in buffers that do not have their own values for
  501.      this variable.  If SYMBOL is not buffer-local, this is
  502.      equivalent to `symbol-value' (*note Accessing Variables::.).
  503.  
  504.  * Special Form: setq-default SYMBOL VALUE
  505.      This sets the default value of SYMBOL to VALUE.  SYMBOL is not
  506.      evaluated, but VALUE is.  The value of the `setq-default' form
  507.      is VALUE.
  508.  
  509.      If a SYMBOL is not buffer-local for the current buffer, and is
  510.      not marked automatically buffer-local, this has the same effect
  511.      as `setq'.  If SYMBOL is buffer-local for the current buffer,
  512.      then this changes the value that other buffers will see (as long
  513.      as they don't have a buffer-local value), but not the value that
  514.      the current buffer sees.
  515.  
  516.           ;; In buffer `foo':
  517.           (make-local-variable 'local)
  518.                => local
  519.           (setq local 'value-in-foo)
  520.                => value-in-foo
  521.           (setq-default local 'new-default)
  522.                => new-default
  523.           local
  524.                => value-in-foo
  525.           (default-value 'local)
  526.                => new-default
  527.           
  528.           ;; In (the new) buffer `bar':
  529.           local
  530.                => new-default
  531.           (default-value 'local)
  532.                => new-default
  533.           (setq local 'another-default)
  534.                => another-default
  535.           (default-value 'local)
  536.                => another-default
  537.           
  538.           ;; Back in buffer `foo':
  539.           local
  540.                => value-in-foo
  541.           (default-value 'local)
  542.                => another-default
  543.  
  544.  * Function: set-default SYMBOL VALUE
  545.      This function is like `setq-default', except that SYMBOL is
  546.      evaluated.
  547.  
  548.           (set-default (car '(a b c)) 23)
  549.                => 23
  550.           (default-value 'a)
  551.                => 23
  552.  
  553.  
  554. 
  555. File: elisp,  Node: Functions,  Next: Macros,  Prev: Variables,  Up: Top
  556.  
  557. Functions
  558. *********
  559.  
  560.    A Lisp program is composed mainly of Lisp functions.  This chapter
  561. explains what functions are, how they accept arguments, and how to
  562. define them.
  563.  
  564. * Menu:
  565.  
  566. * What Is a Function::    Lisp functions vs primitives; terminology.
  567. * Lambda Expressions::    How functions are expressed as Lisp objects.
  568. * Function Names::        A symbol can serve as the name of a function.
  569. * Defining Functions::    Lisp expressions for defining functions.
  570. * Calling Functions::     How to use an existing function.
  571. * Mapping Functions::     Applying a function to each element of a list, etc.
  572. * Anonymous Functions::   Lambda-expressions are functions with no names.    
  573. * Function Cells::        Accessing or setting the function definition
  574.                             of a symbol.
  575. * Related Topics::        Cross-references to specific Lisp primitives
  576.                             that have a special bearing on how functions work.
  577.  
  578.  
  579. 
  580. File: elisp,  Node: What Is a Function,  Next: Lambda Expressions,  Prev: Functions,  Up: Functions
  581.  
  582. What Is a Function?
  583. ===================
  584.  
  585.    In a general sense, a function is a rule for carrying on a
  586. computation given several values called "arguments".  The result of
  587. the computation is called the value of the function.  The computation
  588. can also have side effects: lasting changes in the values of
  589. variables or the contents of data structures.
  590.  
  591.    Here are important terms for functions in Emacs Lisp and for other
  592. function-like objects.
  593.  
  594. "function"
  595.      In Emacs Lisp, a "function" is anything that can be applied to
  596.      arguments in a Lisp program.  In some cases, we will use it more
  597.      specifically to mean a function written in Lisp.  Special forms
  598.      and macros are not functions.
  599.  
  600. "primitive"
  601.      A "primitive" is a function callable from Lisp that is written
  602.      in C, such as `car' or `append'.  These functions are also
  603.      called "built-in" functions or "subrs".  (Special forms are also
  604.      considered primitives.)
  605.  
  606.      Primitives provide the lowest-level interfaces to editing
  607.      functions or operating system services, or in a few cases they
  608.      perform important operations more quickly than a Lisp program
  609.      could.  Primitives can be modified or added only by changing the
  610.      C sources and recompiling the editor.  See *Note Writing Emacs
  611.      Primitives::.
  612.  
  613. "lambda expression"
  614.      A "lambda expression" is a function written in Lisp.  These are
  615.      described in the following section.
  616.  
  617.         *Note Lambda Expressions::.
  618.  
  619. "special form"
  620.      A "special form" is a primitive that is like a function but does
  621.      not evaluate all of its arguments in the usual way.  It may
  622.      evaluate only some of the arguments, or may evaluate them in an
  623.      unusual order, or several times.  Many special forms are
  624.      described in *Note Control Structures::.
  625.  
  626. "macro"
  627.      A "macro" is a construct defined in Lisp by the programmer.  It
  628.      differs from a function in that it translates a Lisp expression
  629.      that you write into an equivalent expression to be evaluated
  630.      instead of the original expression.  *Note Macros::, for how to
  631.      define and use macros.
  632.  
  633. "command"
  634.      A "command" is an object that `command-execute' can invoke; it
  635.      is a possible definition for a key sequence.  Some functions are
  636.      commands; a function written in Lisp is a command if it contains
  637.      an interactive declaration (*note Defining Commands::.).  Such a
  638.      function can be called from Lisp expressions like other
  639.      functions; in this case, the fact that the function is a command
  640.      makes no difference.
  641.  
  642.      Strings are commands also, even though they are not functions. 
  643.      A symbol is a command if its function definition is a command;
  644.      such symbols can be invoked with `M-x'.  The symbol is a
  645.      function as well if the  definition is a function.  *Note
  646.      Command Overview::.
  647.  
  648. "keystroke command"
  649.      A "keystroke command" is a command that is bound to a key
  650.      sequence (typically one to three keystrokes).  The distinction
  651.      is made here merely to avoid confusion with the meaning of
  652.      "command" in non-Emacs editors; for programmers, the distinction
  653.      is normally unimportant.
  654.  
  655.  * Function: subrp OBJECT
  656.      This function returns `t' if OBJECT is a built-in function (i.e.
  657.      a Lisp primitive).
  658.  
  659.           (subrp 'message)                ; `message' is a symbol,
  660.                => nil                     ; not a subr object.
  661.           (subrp (symbol-function 'message))
  662.                => t
  663.  
  664.  
  665. 
  666. File: elisp,  Node: Lambda Expressions,  Next: Function Names,  Prev: What Is a Function,  Up: Functions
  667.  
  668. Lambda Expressions
  669. ==================
  670.  
  671.    A function written in Lisp is a list that looks like this:
  672.  
  673.      (lambda (ARG-VARIABLES...)
  674.        [DOCUMENTATION-STRING]
  675.        [INTERACTIVE-DECLARATION]
  676.        BODY-FORMS...)
  677.  
  678. (Such a list is called a "lambda expression", even though it is not
  679. an expression at all, for historical reasons.)
  680.  
  681. * Menu:
  682.  
  683. * Lambda Components::       The parts of a lambda expression.
  684. * Simple Lambda::           A simple example.
  685. * Argument List::           Details and special features of argument lists.
  686. * Function Documentation::  How to put documentation in a function.
  687.  
  688.  
  689. 
  690. File: elisp,  Node: Lambda Components,  Next: Simple Lambda,  Prev: Lambda Expressions,  Up: Lambda Expressions
  691.  
  692. Components of a Lambda Expression
  693. ---------------------------------
  694.  
  695.    A function written in Lisp (a "lambda expression") is a list that
  696. looks like this:
  697.  
  698.      (lambda (ARG-VARIABLES...)
  699.        [DOCUMENTATION-STRING]
  700.        [INTERACTIVE-DECLARATION]
  701.        BODY-FORMS...)
  702.  
  703.    The first element of a lambda expression is always the symbol
  704. `lambda'.  This indicates that the list represents a function.  The
  705. reason functions are defined to start with `lambda' is so that other
  706. lists, intended for other uses, will not accidentally be valid as
  707. functions.
  708.  
  709.    The second element is a list of argument variable names (symbols).
  710. This is called the "lambda list".  When a Lisp function is called,
  711. the argument values are matched up against the variables in the
  712. lambda list, which are given local bindings with the values provided.
  713. *Note Local Variables::.
  714.  
  715.    The documentation string is an actual string that serves to
  716. describe the function for the Emacs help facilities.  *Note Function
  717. Documentation::.
  718.  
  719.    The interactive declaration is a list of the form `(interactive
  720. CODE-STRING)'.  This declares how to provide arguments if the
  721. function is used interactively.  Functions with this declaration are
  722. called "commands"; they can be called using `M-x' or bound to a key. 
  723. Functions not intended to be called in this way should not have
  724. interactive declarations.  *Note Defining Commands::, for how to
  725. write an interactive declaration.
  726.  
  727.    The rest of the elements are the "body" of the function: the Lisp
  728. code to do the work of the function (or, as a Lisp programmer would
  729. say, "a list of Lisp forms to evaluate").  The value returned by the
  730. function is the value returned by the last element of the body.
  731.  
  732.  
  733. 
  734. File: elisp,  Node: Simple Lambda,  Next: Argument List,  Prev: Lambda Components,  Up: Lambda Expressions
  735.  
  736. A Simple Lambda-Expression Example
  737. ----------------------------------
  738.  
  739.    Consider for example the following function:
  740.  
  741.      (lambda (a b c) (+ a b c))
  742.  
  743. We can call this function by writing it as the CAR of an expression,
  744. like this:
  745.  
  746.      ((lambda (a b c) (+ a b c))
  747.       1 2 3)
  748.  
  749. The body of this lambda expression is evaluated with the variable `a'
  750. bound to 1, `b' bound to 2, and `c' bound to 3.  Evaluation of the
  751. body adds these three numbers, producing the result 6; therefore,
  752. this call to the function returns the value 6.
  753.  
  754.    Note that the arguments can be the results of other function
  755. calls, as in this example:
  756.  
  757.      ((lambda (a b c) (+ a b c))
  758.       1 (* 2 3) (- 5 4))
  759.  
  760. Here all the arguments `1', `(* 2 3)', and `(- 5 4)' are evaluated,
  761. left to right.  Then the lambda expression is applied to the argument
  762. values 1, 6 and 1 to produce the value 8.
  763.  
  764.    It is not often useful to write a lambda expression as the CAR of
  765. a form in this way.  You can get the same result, of making local
  766. variables and giving them values, using the special form `let' (*note
  767. Local Variables::.).  And `let' is clearer and easier to use.  In
  768. practice, lambda expressions are either stored as the function
  769. definitions of symbols, to produce named functions, or passed as
  770. arguments to other functions (*note Anonymous Functions::.).
  771.  
  772.    However, calls to explicit lambda expressions were very useful in
  773. the old days of Lisp, before the special form `let' was invented.  At
  774. that time, they were the only way to bind and initialize local
  775. variables.
  776.  
  777.  
  778. 
  779. File: elisp,  Node: Argument List,  Next: Function Documentation,  Prev: Simple Lambda,  Up: Lambda Expressions
  780.  
  781. Advanced Features of Argument Lists
  782. -----------------------------------
  783.  
  784.    Our simple sample function, `(lambda (a b c) (+ a b c))',
  785. specifies three argument variables, so it must be called with three
  786. arguments: if you try to call it with only two arguments or four
  787. arguments, you will get a `wrong-number-of-arguments' error.
  788.  
  789.    It is often convenient to write a function that allows certain
  790. arguments to be omitted.  For example, the function `substring'
  791. accepts three arguments--a string, the start index and the end
  792. index--but the third argument defaults to the end of the string if
  793. you omit it.  It is also convenient for certain functions to accept
  794. an indefinite number of arguments, as the functions `and' and `+' do.
  795.  
  796.    To specify optional arguments that may be omitted when a function
  797. is called, simply include the keyword `&optional' before the optional
  798. arguments.  To specify a list of zero or more extra arguments,
  799. include the keyword `&rest' before one final argument.
  800.  
  801.    Thus, the complete syntax for an argument list is as follows:
  802.  
  803.      (REQUIRED-VARS...
  804.       [&optional OPTIONAL-VARS...]
  805.       [&rest REST-VAR])
  806.  
  807. The square brackets indicate that the `&optional' and `&rest'
  808. clauses, and the variables that follow them, are optional.
  809.  
  810.    A call to the function requires one actual argument for each of
  811. the REQUIRED-VARS.  There may be actual arguments for zero or more of
  812. the OPTIONAL-VARS, and there cannot be any more actual arguments than
  813. these unless `&rest' exists.  In that case, there may be any number
  814. of extra actual arguments.
  815.  
  816.    If actual arguments for the optional and rest variables are
  817. omitted, then they always default to `nil'.  However, the body of the
  818. function is free to consider `nil' an abbreviation for some other
  819. meaningful value.  This is what `substring' does; `nil' as the third
  820. argument means to use the length of the string supplied.  There is no
  821. way for the function to distinguish between an explicit argument of
  822. `nil' and an omitted argument.
  823.  
  824.      Common Lisp note: Common Lisp allows the function to specify
  825.      what default values will be used when an optional argument is
  826.      omitted; GNU Emacs Lisp always uses `nil'.
  827.  
  828.    For example, an argument list that looks like this:
  829.  
  830.      (a b &optional c d &rest e)
  831.  
  832. binds `a' and `b' to the first two actual arguments, which are
  833. required.  If one or two more arguments are provided, `c' and `d' are
  834. bound to them respectively; any arguments after the first four are
  835. collected into a list and `e' is bound to that list.  If there are
  836. only two arguments, `c' is `nil'; if two or three arguments, `d' is
  837. `nil'; if four arguments or fewer, `e' is `nil'.
  838.  
  839.    There is no way to have required arguments following optional
  840. ones--it would not make sense.  To see why this must be so, suppose
  841. that `c' in the example were optional and `d' were required.  If
  842. three actual arguments are given; then which variable would the third
  843. argument be for?  Similarly, it makes no sense to have any more
  844. arguments (either required or optional) after a `&rest' argument.
  845.  
  846.    Here are some examples of argument lists and proper calls:
  847.  
  848.      ((lambda (n) (1+ n))                ; One required:
  849.       1)                                 ; requires exactly one argument.
  850.           => 2
  851.      ((lambda (n &optional n1)           ; One required and one optional:
  852.               (if n1 (+ n n1) (1+ n)))   ; 1 or 2 arguments.
  853.       1 2)
  854.           => 3
  855.      ((lambda (n &rest ns)               ; One required and one rest:
  856.               (+ n (apply '+ ns)))       ; 1 or more arguments.
  857.       1 2 3 4 5)
  858.           => 15
  859.  
  860.  
  861. 
  862. File: elisp,  Node: Function Documentation,  Prev: Argument List,  Up: Lambda Expressions
  863.  
  864. Documentation Strings of Functions
  865. ----------------------------------
  866.  
  867.    A lambda expression may optionally have a "documentation string"
  868. just after the lambda list.  This string does not affect execution of
  869. the function; it is a kind of comment, but a systematized comment
  870. which actually appears inside the Lisp world and can be used by the
  871. Emacs help facilities.  *Note Documentation::, for how the
  872. DOCUMENTATION-STRING is accessed.
  873.  
  874.    It is a good idea to provide documentation strings for all
  875. commands, and for all other functions in your program that users of
  876. your program should know about; internal functions might as well have
  877. only comments, since comments don't take up any room when your
  878. program is loaded.
  879.  
  880.    The first line of the documentation string should stand on its
  881. own, because `apropos' displays just this first line.  It should
  882. consist of one or two complete sentences that summarize the
  883. function's purpose.
  884.  
  885.    The start of the documentation string is usually indented, but
  886. since these spaces come before the starting double-quote, they are
  887. not part of the string.  Some people make a practice of indenting any
  888. additional lines of the string so that the text lines up.  *This is a
  889. mistake.*  The indentation of the following lines is inside the
  890. string; what looks nice in the source code will look ugly when
  891. displayed by the help commands.
  892.  
  893.    You may wonder how the documentation string could be optional,
  894. since there are required components of the function that follow it
  895. (the body).  Since evaluation of a string returns that string,
  896. without any side effects, it has no effect if it is not the last form
  897. in the body.  Thus, in practice, there is no confusion between the
  898. first form of the body and the documentation string; if the only body
  899. form is a string then it serves both as the return value and as the
  900. documentation.
  901.  
  902.  
  903. 
  904. File: elisp,  Node: Function Names,  Next: Defining Functions,  Prev: Lambda Expressions,  Up: Functions
  905.  
  906. Naming a Function
  907. =================
  908.  
  909.    In most computer languages, every function has a name; the idea of
  910. a function without a name is nonsensical.  In Lisp, a function in the
  911. strictest sense has no name.  It is simply a list whose first element
  912. is `lambda', or a primitive subr-object.
  913.  
  914.    However, a symbol can serve as the name of a function.  This
  915. happens when you put the function in the symbol's "function cell"
  916. (*note Symbol Components::.).  Then the symbol itself becomes a
  917. valid, callable function, equivalent to the list or subr-object that
  918. its function cell refers to.  The contents of the function cell are
  919. also called the symbol's "function definition".
  920.  
  921.    In practice, nearly all functions are given names in this way and
  922. referred to through their names.  For example, the symbol `car' works
  923. as a function and does what it does because the primitive subr-object
  924. `#<subr car>' is stored in its function cell.
  925.  
  926.    We give functions names because it is more convenient to refer to
  927. them by their names in other functions.  For primitive subr-objects
  928. such as `#<subr car>', names are the only way you can refer to them:
  929. there is no read syntax for such objects.  For functions written in
  930. Lisp, the name is more convenient to use in a call than an explicit
  931. lambda expression.  Also, a function with a name can refer to
  932. itself--it can be recursive.  Writing the function's name in its own
  933. definition is much more convenient than making the function
  934. definition point to itself (something that is not impossible but that
  935. has various disadvantages in practice).
  936.  
  937.    Functions are often identified with the symbols used to name them.
  938. For example, we often speak of "the function `car'", not
  939. distinguishing between the symbol `car' and the primitive subr-object
  940. that is its function definition.  For most purposes, there is no need
  941. to distinguish.
  942.  
  943.    Even so, keep in mind that a function need not have a unique name.
  944. While a given function object *usually* appears in the function cell
  945. of only one symbol, this is just a matter of convenience.  It is very
  946. easy to store it in several symbols using `fset'; then each of the
  947. symbols is equally well a name for the same function.
  948.  
  949.    A symbol used as a function name may also be used as a variable;
  950. these two uses of a symbol are independent and do not conflict.
  951.  
  952.  
  953. 
  954. File: elisp,  Node: Defining Functions,  Next: Calling Functions,  Prev: Function Names,  Up: Functions
  955.  
  956. Defining Named Functions
  957. ========================
  958.  
  959.    We usually give a name to a function when it is first created. 
  960. This is called "defining a function", and it is done with the `defun'
  961. special form.
  962.  
  963.  * Special Form: defun NAME ARGUMENT-LIST BODY-FORMS
  964.      `defun' is the usual way to define new Lisp functions.  It
  965.      defines the symbol NAME as a function that looks like this:
  966.  
  967.           (lambda ARGUMENT-LIST . BODY-FORMS)
  968.  
  969.      This lambda expression is stored in the function cell of NAME. 
  970.      The value returned by evaluating the `defun' form is NAME, but
  971.      usually we ignore this value.
  972.  
  973.      As described previously (*note Lambda Expressions::.),
  974.      ARGUMENT-LIST is a list of argument names and may include the
  975.      keywords `&optional' and `&rest'.  Also, the first two forms in
  976.      BODY-FORMS may be a documentation string and an interactive
  977.      declaration.
  978.  
  979.      Note that the same symbol NAME may also be used as a global
  980.      variable, since the value cell is independent of the function
  981.      cell.
  982.  
  983.      Here are some examples:
  984.  
  985.           (defun foo () 5)
  986.                => foo
  987.           (foo)
  988.                => 5
  989.           
  990.           (defun bar (a &optional b &rest c)
  991.               (list a b c))
  992.                => bar
  993.           (bar 1 2 3 4 5)
  994.                => (1 2 (3 4 5))
  995.           (bar 1)
  996.                => (1 nil nil)
  997.           (bar)
  998.           error--> Wrong number of arguments.
  999.           
  1000.           (defun capitalize-backwards ()
  1001.             "This function makes the last letter of a word upper case."
  1002.             (interactive)
  1003.             (backward-word 1)
  1004.             (forward-word 1)
  1005.             (backward-char 1)
  1006.             (capitalize-word 1))
  1007.                => capitalize-backwards
  1008.  
  1009.      Be careful not to redefine existing functions unintentionally. 
  1010.      `defun' will redefine even primitive functions such as `car'
  1011.      without any hesitation or notification.  Redefining a function
  1012.      already defined is often done deliberately, and there is no way
  1013.      to distinguish deliberate redefinition from unintentional
  1014.      redefinition.
  1015.  
  1016.  
  1017. 
  1018. File: elisp,  Node: Calling Functions,  Next: Mapping Functions,  Prev: Defining Functions,  Up: Functions
  1019.  
  1020. Calling Functions
  1021. =================
  1022.  
  1023.    Defining functions is only half the battle.  Functions don't do
  1024. anything until you "call" them, i.e., tell them to run.  This process
  1025. is also known as "invocation".
  1026.  
  1027.    The most common way of invoking a function is by evaluating a
  1028. list.  For example, evaluating the list `(concat "a" "b")' calls the
  1029. function `concat'.  *Note Evaluation::, for a description of
  1030. evaluation.
  1031.  
  1032.    When you write a list as an expression in your program, the
  1033. function name is part of the program.  This means that the choice of
  1034. which function to call is made when you write the program.  Usually
  1035. that's just what you want.  Occasionally you need to decide at run
  1036. time which function to call.  Then you can use the functions
  1037. `funcall' and `apply'.
  1038.  
  1039.  * Function: funcall FUNCTION &rest ARGUMENTS
  1040.      `funcall' calls FUNCTION with ARGUMENTS, and returns whatever
  1041.      FUNCTION returns.
  1042.  
  1043.      Since `funcall' is a function, all of its arguments, including
  1044.      FUNCTION, are evaluated before `funcall' is called.  This means
  1045.      that you can use any expression to obtain the function to be
  1046.      called.  It also means that `funcall' does not see the
  1047.      expressions you write for the ARGUMENTS, only their values. 
  1048.      These values are *not* evaluated a second time in the act of
  1049.      calling FUNCTION; `funcall' enters the normal procedure for
  1050.      calling a function at the place where the arguments have already
  1051.      been evaluated.
  1052.  
  1053.      The argument FUNCTION must be either a Lisp function or a
  1054.      primitive function.  Special forms and macros are not allowed,
  1055.      because they make sense only when given the "unevaluated"
  1056.      argument expressions.  `funcall' cannot provide these because,
  1057.      as we saw above, it never knows them in the first place.
  1058.  
  1059.           (setq f 'list)
  1060.                => list
  1061.           (funcall f 'x 'y 'z)
  1062.                => (x y z)
  1063.           (funcall f 'x 'y '(z))
  1064.                => (x y (z))
  1065.           (funcall 'and t nil)
  1066.           error--> Invalid function: #<subr and>
  1067.  
  1068.      Compare this example with that of `apply'.
  1069.  
  1070.  * Function: apply FUNCTION &rest ARGUMENTS
  1071.      `apply' calls FUNCTION with ARGUMENTS, just like `funcall' but
  1072.      with one difference: the last of ARGUMENTS is a list of
  1073.      arguments to give to FUNCTION, rather than a single argument. 
  1074.      We also say that this list is "appended" to the other arguments.
  1075.  
  1076.      `apply' returns the result of calling FUNCTION.  As with
  1077.      `funcall', FUNCTION must either be a Lisp function or a
  1078.      primitive function; special forms and macros do not make sense
  1079.      in `apply'.
  1080.  
  1081.           (setq f 'list)
  1082.                => list
  1083.           (apply f 'x 'y 'z)
  1084.           error--> Wrong type argument: listp, z
  1085.           (apply '+ 1 2 '(3 4))
  1086.                => 10
  1087.           (apply '+ '(1 2 3 4))
  1088.                => 10
  1089.           
  1090.           (apply 'append '((a b c) nil (x y z) nil))
  1091.                => (a b c x y z)
  1092.  
  1093.      An interesting example of using `apply' is found in the
  1094.      description of `mapcar'; see the following section.
  1095.  
  1096.    It is common for Lisp functions to accept functions as arguments
  1097. or find them in data structures (especially in hook variables and
  1098. property lists) and call them using `funcall' or `apply'.  Functions
  1099. that accept function arguments are often called "functionals".
  1100.  
  1101.    Sometimes, when you call such a function, it is useful to supply a
  1102. no-op function as the argument.  Here are two different kinds of
  1103. no-op function:
  1104.  
  1105.  * Function: identity ARG
  1106.      This function returns ARG and has no side effects.
  1107.  
  1108.  * Function: ignore &rest ARGS
  1109.      This function ignores any arguments and returns `nil'.
  1110.  
  1111.  
  1112. 
  1113. File: elisp,  Node: Mapping Functions,  Next: Anonymous Functions,  Prev: Calling Functions,  Up: Functions
  1114.  
  1115. Mapping Functions
  1116. =================
  1117.  
  1118.    A "mapping function" applies a given function to each element of a
  1119. list or other collection.  Emacs Lisp has three such functions;
  1120. `mapcar' and `mapconcat', which scan a list, are described here.  For
  1121. the third mapping function, `mapatoms', see *Note Creating Symbols::.
  1122.  
  1123.  * Function: mapcar FUNCTION SEQUENCE
  1124.      `mapcar' applies FUNCTION to each element of SEQUENCE in turn. 
  1125.      The results are made into a `nil'-terminated list.
  1126.  
  1127.      The argument SEQUENCE may be a list, a vector or a string.  The
  1128.      result is always a list.  The length of the result is the same
  1129.      as the length of SEQUENCE.
  1130.  
  1131.      For example:
  1132.  
  1133.           (mapcar 'car '((a b) (c d) (e f)))
  1134.                => (a c e)
  1135.           (mapcar '1+ [1 2 3])
  1136.                => (2 3 4)
  1137.           (mapcar 'char-to-string "abc")
  1138.                => ("a" "b" "c")
  1139.           
  1140.           ;; Call each function in `my-hooks'.
  1141.           (mapcar 'funcall my-hooks)
  1142.           
  1143.           (defun mapcar* (f &rest args)
  1144.             "Apply FUNCTION to successive cars of all ARGS, until one ends.
  1145.           Return the list of results."
  1146.             (if (not (memq 'nil args))              ; If no list is exhausted,
  1147.                 (cons (apply f (mapcar 'car args))  ; Apply function to CARs.
  1148.                       (apply 'mapcar* f             ; Recurse for rest of elements.
  1149.                              (mapcar 'cdr args)))))
  1150.           
  1151.           (mapcar* 'cons '(a b c) '(1 2 3 4))
  1152.                => ((a . 1) (b . 2) (c . 3))
  1153.  
  1154.  * Function: mapconcat FUNCTION SEQUENCE SEPARATOR
  1155.      `mapconcat' applies FUNCTION to each element of SEQUENCE: the
  1156.      results, which must be strings, are concatenated.  Between each
  1157.      pair of result strings, `mapconcat' inserts the string
  1158.      SEPARATOR.  Usually SEPARATOR contains a space or comma or other
  1159.      suitable punctuation.
  1160.  
  1161.      The argument FUNCTION must be a function that can take one
  1162.      argument and returns a string.
  1163.  
  1164.           (mapconcat 'symbol-name
  1165.                      '(The cat in the hat)
  1166.                      " ")
  1167.                => "The cat in the hat"
  1168.           
  1169.           (mapconcat (function (lambda (x) (format "%c" (1+ x))))
  1170.                      "HAL-8000"
  1171.                      "")
  1172.                => "IBM.9111"
  1173.  
  1174.  
  1175. 
  1176. File: elisp,  Node: Anonymous Functions,  Next: Function Cells,  Prev: Mapping Functions,  Up: Functions
  1177.  
  1178. Anonymous Functions
  1179. ===================
  1180.  
  1181.    In Lisp, a function is a list that starts with `lambda' (or
  1182. alternatively a primitive subr-object); names are "extra".  Although
  1183. usually functions are defined with `defun' and given names at the
  1184. same time, it is occasionally more concise to use an explicit lambda
  1185. expression--an anonymous function.  Such a list is valid wherever a
  1186. function name is.
  1187.  
  1188.    Any method of creating such a list makes a valid function.  Even
  1189. this:
  1190.  
  1191.      (setq silly (append '(lambda (x)) (list (list '+ (* 3 4) 'x))))
  1192.           => (lambda (x) (+ 12 x))
  1193.  
  1194. This computes a list that looks like `(lambda (x) (+ 12 x))' and
  1195. makes it the value (*not* the function definition!) of `silly'.
  1196.  
  1197.    Here is how we might call this function:
  1198.  
  1199.      (funcall silly 1)
  1200.           => 13
  1201.  
  1202. (It does *not* work to write `(silly 1)', because this function is
  1203. not the *function definition* of `silly'.  We have not given `silly'
  1204. any function definition, just a value as a variable.)
  1205.  
  1206.    Most of the time, anonymous functions are constants that appear in
  1207. your program.  For example, you might want to pass one as an argument
  1208. to the function `mapcar', which applies any given function to each
  1209. element of a list.  Here we pass an anonymous function that
  1210. multiplies a number by two:
  1211.  
  1212.      (defun double-each (list)
  1213.        (mapcar '(lambda (x) (* 2 x)) list))
  1214.           => double-each
  1215.      (double-each '(2 11))
  1216.           => (4 22)
  1217.  
  1218. In such cases, we usually use the special form `function' instead of
  1219. simple quotation to quote the anonymous function.
  1220.  
  1221.  * Special Form: function FUNCTION-OBJECT
  1222.      This special form returns FUNCTION-OBJECT without evaluating it.
  1223.      In this, it is equivalent to `quote'.  However, it serves as a
  1224.      note to the Emacs Lisp compiler that FUNCTION-OBJECT is intended
  1225.      to be used only as a function, and therefore can safely be
  1226.      compiled.  *Note Quoting::, for comparison.
  1227.  
  1228.    Using `function' instead of `quote' makes a difference inside a
  1229. function or macro that you are going to compile.  For example:
  1230.  
  1231.      (defun double-each (list)
  1232.        (mapcar (function (lambda (x) (* 2 x))) list))
  1233.           => double-each
  1234.      (double-each '(2 11))
  1235.           => (4 22)
  1236.  
  1237. If this definition of `double-each' is compiled, the anonymous
  1238. function is compiled as well.  By contrast, in the previous
  1239. definition where ordinary `quote' is used, the argument passed to
  1240. `mapcar' is the precise list shown:
  1241.  
  1242.      (lambda (arg) (+ arg 5))
  1243.  
  1244. The Lisp compiler cannot assume this list is a function, even though
  1245. it looks like one, since it does not know what `mapcar' does with the
  1246. list.  Perhaps `mapcar' will check that the CAR of the third element
  1247. is the symbol `+'!  The advantage of `function' is that it tells the
  1248. compiler to go ahead and compile the constant function.
  1249.  
  1250.    We sometimes write `function' instead of `quote' when quoting the
  1251. name of a function, but this usage is just a sort of comment.
  1252.  
  1253.      (function SYMBOL) == (quote SYMBOL) == 'SYMBOL
  1254.  
  1255.    See `documentation' in *Note Accessing Documentation::, for a
  1256. realistic example using `function' and an anonymous function.
  1257.  
  1258.  
  1259.